<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Email extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('email_m');
		$this->load->helper('date');
	}
	

	
	/*--写信------------------------------------------------------------------------------*/
	public function emailadd(){//新邮件
		$status = $this->uri->segment(3, 0);
		$bodyid = $this->uri->segment(4, 0);
		if($status != 0){
			if($status == 1 || $status == 2){
				$m = 'sent';
			}else if($status == 3){
				$m = 'in';
			}
			$data['arr'] = $this->email_m->contentbox($m,$bodyid,0);
		}
		$data['status'] = $status;//状态 1为发件箱重发邮件，2为收件箱转发邮件，3为回复邮件
		$this->load->view('email/new',$data);
	}
	/*--写信POST提交处理------------------------------------------------------------------------------*/
	public function _emailaddpost_post(){//新邮件
		$arrdata=$this->input->post();
		$data = $this->email_m->emailaddpost($arrdata);
		$this->rejsonsuccess($data,"发送邮件失败，请联系管理员");	
	}
	
	
	/*--收件箱------------------------------------------------------------------------------*/
	public function inbox(){//收件箱视图
		$this->load->view('email/inbox');
	}
	public function _getinboxlistdata_post(){//收件箱JSON数据
		if($this->acl->check_privilege('email_inbox') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$rowdata = '';
			$data = $this->email_m->getinboxlistdata($this->input->post());
			if(!empty($data['data'])){
				foreach ($data['data'] as $v){
					//$senderdate = reset(explode(' ',$v['sendertime']));
					$rowdata[] = array("id"=>$v['emailid'],"important"=>$v['important'],"sender"=>$v['sender'],"title"=>$v['title'],"sendertime"=>$v['sendertime'],"isread"=>$v['read'],"bodyid"=>$v['bodyid'],'isattach'=>$v['attachment_id']);
				}
			}
			$data = array("total"=>$data['total'],'rows'=>$rowdata);
			echo json_encode($data); 
		}
	}
	
	//设置为已读
	public function setread(){
		$id=$this->input->post();
		$data = $this->email_m->setread($id['getid']);
		$this->rejsonsuccess($data,"'标记为已读'失败，请联系管理员");		
	}
	
	//删除邮件
	public function _del_email_post(){
		$data=$this->input->post();
		$data = $this->email_m->del_email($data);
		$this->rejsonsuccess($data,"'删除邮件'失败，请联系管理员");		
	}

	
	/*----发件箱-------------------------------------------------------------------*/
	public function sentbox(){//发件箱视图
		$this->load->view('email/sentbox');
	}
	public function _getsentboxlistdata_post(){//发件箱JSON数据
		if($this->acl->check_privilege('email_sentbox') == false){
			show_error("you have no privilege to access this page");
			return ;
		}else{
			$rowdata = '';
			$data = $this->email_m->getsentboxlistdata($this->input->post());
			if(!empty($data['data'])){
				foreach ($data['data'] as $v){
					//$senderdate = reset(explode(' ',$v->sendertime));
					$rowdata[] = array("id"=>$v['emailid'],"sender"=>$v['sender'],"addressee2"=>$v['addressee2'],"title"=>$v['title'],"sendertime"=>$v['sendertime'],"isread"=>$v['read'],"bodyid"=>$v['bodyid'],'isattach'=>$v['attachment_id']);
				}
			}
			$data = array("total"=>$data['total'],'rows'=>$rowdata);
			echo json_encode($data); 
			
		}
	}
	/*
	public function sentboxdel(){//发件箱删除
		$id=$this->input->post();//当前页数
		$data = $this->email_m->sentboxdel($id['getid']);
		$this->rejsonsuccess($data,"'标记为已读'失败，请联系管理员");		
	}
	*/
	public function sentuserboxdel(){//发件箱删除
		$id=$this->input->post();//当前页数
		$data = $this->email_m->sentuserboxdel($id['getid']);
		$this->rejsonsuccess($data,"'标记为已读'失败，请联系管理员");		
	}
	
	
	
/*---EMAIL详细内容视图---------------------------------------*/
	function contentbox(){
		$m = $this->uri->segment(3, 0);
		$id = $this->uri->segment(4, 0);
		$f = $this->uri->segment(5, 0);//该参数没有时返回该条的JSON数据
		$data['incontentview'] = $this->email_m->contentbox($m,$id,$f);
		if($data['incontentview']){
			if($f === 0){
				$data['attlist'] = $this->attlist('email',$data['incontentview'][0]->attachment_id,$data['incontentview'][0]->attachment_name,$data['incontentview'][0]->size,$m.$data['incontentview'][0]->bodyid);
				$this->load->view('email/'.$m.'contentbox',$data);
			}else{
				//print_r($data['incontentview']);
				echo json_encode(array("success"=>true,"data"=>$data['incontentview'][0]));
			}
		}else{
			echo "<script>var tab = $('#main-tab').tabs('getSelected');var index = $('#main-tab').tabs('getTabIndex',tab);$('#main-tab').tabs('close',index);$.messager.alert('错误','没有找到该邮件，可能已被删除！','error');</script>";
		}
	}
	function get_email_content(){
		$m = $this->uri->segment(3, 0);
		$id = $this->uri->segment(4, 0);
		$incontentview = $this->email_m->contentbox($m,$id);
		//$data['incontentview'] = $this->email_m->uofdContent($f,$emailid);
		
		echo $incontentview[0]->content;
		//echo json_encode(array("success"=>true,"data"=>$incontentview));
		//$this->load->view('email/contentbox',$data);
	}
	function _read_status_post(){//查看收件人详细状态
		if($this->acl->check_privilege('email_sentbox') == false)
		{
			show_error("you have no privilege to access this page");
			return ;
		}else{
			$rowdata = '';
			$bodyid = $this->uri->segment(3, 0);
			$query=$this->email_m->readStatusdata($bodyid);//总条数
			foreach ($query as $v){
				$rowdata[] = array("id"=>$v->emailid,"addressee"=>$v->addressee,"read"=>$v->read,"readtime"=>$v->readtime,"bodyid"=>$v->bodyid,"del"=>$v->delete);
			}
			$listdata = array("total"=>count($query),'rows'=>$rowdata);
			echo json_encode($listdata); 
		}			
	}

	function advanced_search(){//高级搜索功能视图页
		$m = $this->uri->segment(3, 0);
		if($m == 'email_in'){
			$t = '收件箱';
		}else if($m == 'email_sent'){
			$t = '已发送';	
		}
		$data['v'] = array('m'=>$m,'t'=>$t);
		$this->load->view('email/email_search',$data);
	}
	
	

}
?>